<template>
  <div class="gap">
    <h2>保险产品修改</h2>
    <el-form :model="update" status-icon ref="update" label-width="70px" class="demo-update">
      <el-form-item label="名字">
        <el-input type="text" v-model="update.productName" max="250px"></el-input>
      </el-form-item>

      <el-form-item label="价格">
        <el-input type="text" v-model="update.price" max="250px"></el-input>
      </el-form-item>

      <el-form-item label="详情">
        <el-input type="text" v-model="update.detail" max="250px"></el-input>
      </el-form-item>

      <el-form-item label="状态">
        <el-input type="text" v-model="update.state" max="250px"></el-input>
      </el-form-item>

      <el-form-item label="年限">
        <el-input type="text" v-model="update.limittime" max="250px"></el-input>
      </el-form-item>

      <el-form-item label="图片">
        <el-upload
          class="avatar-uploader"
          action="http://localhost:3000/images/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="true" :src="'http://localhost:3000/temp/'+update.imgname" class="avatar" />

          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="图片地址">
        <p>{{update}}</p>
        <p>{{update.imgname}}</p>
      </el-form-item>
      <el-form-item label="种类">
        <Category @choose="choose"></Category>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="updateproduct1(update)">修改保险分类</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Category from "../productcategory/Cascader.vue";
import { createNamespacedHelpers } from "vuex";
const { mapState, mapActions, mapMutations } = createNamespacedHelpers("products");

export default {
  props: ["_id"],
  components: {
    Category,
  },
  data() {
    return {};
  },
  computed: {
    ...mapState(["update"]),
  },
  created() {
    this.getproductid(this._id);
  },
  mounted() {
    console.log(this.update);
  },
  methods: {
    ...mapActions(["updateproduct", "getproductid"]),
    ...mapMutations(['addImgname']),
    async updateproduct1(update) {
      const data = await this.updateproduct(update);
      if (data.status) {
        this.$router.push("/system/productlist");
      }
    },
    choose(val) {
      this.update.categoryId = val;
    },
    //图片
    handleAvatarSuccess(res, file) {
      if (res.status) {
        // this.update.imgname = res.filename;
        this.addImgname(res.filename);
        console.log("图片重新赋值", this.update.imgname);
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="scss">
.gap {
  width: 40%;
  margin: 0px auto;
}
h2 {
  line-height: 60px;
}
.form {
  width: 290px;
  margin: 0px auto;
}
.el-input__inner {
  width: 250px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>